package com.lry.basic.jvm;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @author:刘仁有
 * @desc: isSynthetic 是为了解决私有变量只能本类访问和外部类可以访问内部类的私有变量的矛盾产生的
 * 我们手写的都是false，jvm虚拟机产生的是true（合成的）
 * @email:953506233@qq.com
 * @data:2019/12/4
 */
public class TestSynthetic {
    public static void main(String[] args) {
        Inner inner = new Inner();
        System.out.println(inner.i);
        System.out.println(inner.m());
        print(Inner.class);
    }
    private static void print(Class clazz){
        System.out.println("------fields------");
        Field[] fields = clazz.getDeclaredFields();
        for (Field field:fields) {
            System.out.println(field.isSynthetic()+" "+field.getType()+"  "+field.getName());
        }
        System.out.println("------methods------");
        Method[]methods = clazz.getDeclaredMethods();
        for (Method method:methods) {
            System.out.println(method.isSynthetic()+" "+method.getReturnType()+" "+method.getName());
        }
        System.out.println("------constructs------");
        Constructor<?>[] cons = clazz.getDeclaredConstructors();
        for (Constructor con:cons) {
            System.out.println(con.isSynthetic()+" "+con.getName());
        }
    }
    static class Inner{
        private Inner(){}
        private int i;
        private int m(){
            return 1;
        }
    }
}
